import json

import pandas as pd
import xlrd

from config import doc_data_dir, doc_dict_dir, HOTLINE_SIDU_TO_PREDICT
from src import difficulty_predictV2


def predict():
    workbook = xlrd.open_workbook(INPUT_FILENAME, "w+")
    table1 = workbook.sheets()[SHEET]
    # for i in range(1, table1.nrows):
    content_list = []
    difficulty_list = []
    print(table1.nrows)
    #  打开难度库
    f = open(DIFFICULTY_LIBRARY, "r+", encoding="utf-8")
    info = json.load(f)
    for i in range(1, table1.nrows):
        dict_1 = dict()
        list_2 = []
        dict_3 = dict()
        list_4 = []
        # 内容分类
        str1 = table1.cell_value(i, CONTENT_CLASSIFICATION_COL_level_1)
        str2 = table1.cell_value(i, CONTENT_CLASSIFICATION_COL_level_2)
        str3 = table1.cell_value(i, CONTENT_CLASSIFICATION_COL_level_3)
        string = str1 + "_" + str2 + "_" + str3
        dict_1['name'] = table1.cell_value(i, INCIDENT_COL)  # 标题
        list_2.append(dict_1)
        dict_3['type'] = string
        dict_3['keyword'] = list_2
        #  #######
        for j in info:
            if string == j['type']:
                dict_3['type_count'] = j['type_count']
                break
        # 如果输入的type不在库里，直接将其赋值为最大长度+2，让其直接在dict_3中找不到对应的count，赋值为默认值
        else:
            dict_3['type_count'] = len(info) + 2
        #  ######
        list_4.append(dict_3)
        input_data = list_4
        content_list.append(table1.cell_value(i, INCIDENT_COL))
        result = difficulty_predictV2.PredictDiff.get_result(input_data[0], difficulty_library=DIFFICULTY_LIBRARY)
        difficulty_list.append(result)
        print("第", i, "个:", result)
        # print(list_4)
        # print(list_4[0]['keyword'][0]['name'])
    res_pd = pd.DataFrame({'热线标题': content_list, '难度': difficulty_list})
    res_pd.to_excel(OUTPUT_FILENAME, encoding="utf-8")


if __name__ == '__main__':
    # 几个参数
    # INPUT_FILENAME（输入的EXCEL文件名）
    INPUT_FILENAME = doc_data_dir + HOTLINE_SIDU_TO_PREDICT
    # DIFFICULTY_LIBRARY(选择的难度库)
    DIFFICULTY_LIBRARY = doc_dict_dir + '1.json'
    # OUTPUT_FILENAME(输出规则库的文件名)
    OUTPUT_FILENAME = doc_data_dir + 'hash_search最新1月份热线难度预测.xls'
    # 第几个sheet
    SHEET = 0
    # 内容分类的列数
    CONTENT_CLASSIFICATION_COL_level_1 = 4
    CONTENT_CLASSIFICATION_COL_level_2 = 5
    CONTENT_CLASSIFICATION_COL_level_3 = 6
    # 实体事件的列数
    INCIDENT_COL = 7
    predict()
